package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.entity.question.QuestionTopic;
import com.everflourish.yeah100.http.ImageRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/25.
 * 时间：2016/7/25
 */
public class QuestionUploadAdapter extends RecyclerView.Adapter<QuestionUploadAdapter.QuestionUploadHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<QuestionTopic> mList;
    private ImageRequest imageRequest;
    private RequestQueue mQueue;
    private Uri croUrl;
    private Bitmap mUploadBitmap;

    public QuestionUploadAdapter(Context mContext, List<QuestionTopic> mList, Bitmap mUploadBitmap) {
        this.mContext = mContext;
        this.mList = (ArrayList<QuestionTopic>) mList;
        this.mUploadBitmap = mUploadBitmap;
        mLayoutInflater = LayoutInflater.from(mContext);
        mQueue = Volley.newRequestQueue(mContext);
        imageRequest = ImageRequest.getInstance(mQueue);
    }

    @Override
    public QuestionUploadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_question_topic, parent, false);
        QuestionUploadHolder viewHolder = new QuestionUploadHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(QuestionUploadHolder holder, final int position) {
        QuestionTopic questionTopic = mList.get(position);
        if (mList.size() != 0 && position == 0) {
            holder.questionIv.setImageBitmap(mUploadBitmap);
        } else {
            holder.questionIv.setVisibility(View.GONE);
        }
        QuestionUploadItemAdapter questionUploadItemAdapter = new QuestionUploadItemAdapter(
                mContext, questionTopic.getQuestionNos(), questionTopic.getTopicType());
        holder.questionTypeTv.setText(questionTopic.getTopicName());
        holder.questionNoRv.setAdapter(questionUploadItemAdapter);
        holder.questionNoRv.setLayoutManager(new GridLayoutManager(mContext, 5));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class QuestionUploadHolder extends RecyclerView.ViewHolder {

        private TextView questionTypeTv;
        private RecyclerView questionNoRv;
        private ImageView questionIv;

        public QuestionUploadHolder(View itemView) {
            super(itemView);
            questionNoRv = (RecyclerView) itemView.findViewById(R.id.question_topic_select_rv);
            questionTypeTv = (TextView) itemView.findViewById(R.id.question_topic_type);
            questionIv = (ImageView) itemView.findViewById(R.id.item_question_topic_image);
        }
    }

}